#ifdef __APPLE__
    #include <SDL/SDL.h>
#else
    #include <SDL/SDL.h>
#endif
#include "BM_config.h"

BMConfig::BMConfig(){
    use_joystick = 0;
}

/*******************************************************************************/
int BMConfig::readConfig(char * src,ConfigStruct* config ){
    FILE *file;

    int charn = 0, readingName = 1, readingData = 0, curindex = 0;

    file = fopen(src, "r"); // open our config file
    if (!file)
        return 0;

    char c;

    do
	{
		c = fgetc(file);

		if (c == '\0' || c == ' ' || c == '\t' || c == '#') // skip spaces, comments, tabs, end of line
			continue;
		else if (c == '=')
		{
			if (readingName == 1 && readingData == 0)
			{
				readingName = 0;
				readingData = 1;
				charn = 0;
			}
		}
		else if (c == '\n') // UNIX systems
		{
			if (readingName == 0 && readingData == 1)
			{
				readingName = 1;
				readingData = 0;
				charn = 0;
				curindex++;
			}
		}
		else if (c == '\r') // Windows :P (\r\n)
		{
			if (readingName == 0 && readingData == 1)
			{
				readingName = 1;
				readingData = 0;
				charn = 0;
				curindex++;
			}
		}
		else {
			if (readingName == 1 && readingData == 0)
			{
				config[curindex].setting_name[charn] = c;
				charn++;
			}
			else if (readingName == 0 && readingData == 1)
			{
				config[curindex].setting_data[charn] = c;
				charn++;
			}
		}

		/*endGfx();
		printf("c: %c\ncharn: %d\ncurindex: %d\ntempCfg.name: %s\ntempCfg.data: %s", c, charn, curindex, tempCfg[curindex].name, tempCfg[curindex].data);
		WaitX();
		initGraphics();*/

	} while (c != EOF);

    fclose(file);

    return 1;
}
